package action;

import config.ApplicationConfig;
import net.sf.json.JSONObject;
import server.HistoryServer;
import server.RegisterServer;
import server.SettingServer;
import util.Decode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-03-11 Monday 15:47:33
 */
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        session.setAttribute("infoTitle", "用户注册");
        session.setAttribute("panelColor", "blue");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String again = request.getParameter("again");
        String verificationCode = request.getParameter("ver_code");
        String cookie = session.getAttribute("cookie").toString();

        String returnPage = request.getParameter("return");
        JSONObject result = JSONObject.fromObject("{}");

        SettingServer settingServer = new SettingServer();
        settingServer.open();
        boolean flag = settingServer.readSettingObject("register").getBoolean("registrable");
        settingServer.close();

        if (flag) {
            if (username != null && !username.equals("") &&
                    password != null && !password.equals("") &&
                    again != null && !again.equals("") &&
                    verificationCode != null && !verificationCode.equals("") &&
                    returnPage != null && !returnPage.equals("") &&
                    cookie != null && !cookie.equals("")) {

                Decode decode = new Decode();
                username = decode.urlUtf8Decode(username);
                password = decode.urlUtf8Decode(password);
                again = decode.urlUtf8Decode(again);
                verificationCode = decode.urlUtf8Decode(verificationCode);
                returnPage = decode.urlUtf8Decode(returnPage);
                cookie = decode.urlUtf8Decode(cookie);

                RegisterServer registerServer = new RegisterServer();
                registerServer.open();
                result = registerServer.register(username, password, again, cookie, verificationCode);
                registerServer.close();
                if (result.getString("state").equals("false")) {
                    session.setAttribute("panelColor", "red");
                    session.setAttribute("infoContent", "注册失败，该用户名可能已被使用，请联系管理员修改密码");
                } else {
                    session.setAttribute("infoContent", "注册成功，点击返回按钮返回首页");
                }
                session.setAttribute("return", returnPage);
            } else {
                session.setAttribute("panelColor", "red");
                session.setAttribute("infoContent", "注册失败，信息不能为空");
                session.setAttribute("return", "index");
            }
        } else {
            session.setAttribute("panelColor", "red");
            session.setAttribute("infoContent", "注册失败，注册通道已被关闭，请联系管理员");
            session.setAttribute("return", "index");
        }

        HistoryServer historyServer = new HistoryServer();
        historyServer.open();
        historyServer.record(result.has("userID") ? result.getString("userID") : "", username + "尝试注册" + (result.has("content") ? "，" + result.getString("content") : ""), request);
        historyServer.close();
        response.sendRedirect("information.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
